<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2018/12/6
 * Time: 17:05
 */

namespace app\login\controller;

use app\common\controller\Base;
use think\Db;
use think\Validate;
use tools\curl\Curl;

/**
 * @title 登录页面（模块02-01）
 * @description 登录说明
 * @group 登录（02）
 * @header name:model_id require:0 default:02-01 desc:模块
 * @groupremark 已使用错误码：1-02-01-0001 校验失败 1-02-01-0002 添加失败 1-02-01-0003 编辑校验失败 1-02-01-0004 编辑失败 1-02-01-0005 获取详情失败  1-02-01-0006 获取列表失败 1-02-01-0007获取单个详情失败 1-02-01-0008 删除失败
 */
class Login extends Base
{
    /**
     * @title 后台登录
     * @description 后台登录
     * @author 李锐志
     * @url /login/login/index
     * @method POST
     * @param name:username type:int require:1 default:1 other: desc:账号
     * @param name:password type:int require:1 default:1 other: desc:密码
     * @return data:帐号信息@!
     * @data token:token
     *
     */
    public function index()
    {
        $data = input();

        $validate = new Validate([
            'username' => 'require',
            'password' => 'require',
        ], [
            'username' => '账号不能为空',
            'password' => '密码不能为空',
        ]);

        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        $account_model_api = Config('model_api.user');
        $url = $account_model_api . "/admin_user/info";
        $account = Curl::get($url, array('token' => ROOT_TOKEN, 'username' => $data['username']));
        $account_status = Curl::json2arr($account)['status'];

        //判断账号是否存在
        if (!$account_status) {
            $msg['msg'] = 'Account does not exist!';
            return $this->errorJson($msg);
        }

        $account = Curl::json2arr($account)['data'];

        if ($account['status'] == 0) {
            $msg['msg'] = 'The account has been disabled!';
            return $this->errorJson($msg);
        }

        //账号存在,验证密码
        if ($account['password'] === md5($data['password'])) {
            //随机生成20位字符串
            $token_str = substr(str_shuffle('abcedfghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 20);

            //token表需要的字段
            $token['user_id'] = $account['id'];
            $token['token'] = $token_str;
            $token['expires_in'] = 3600;             //有效期60分钟
            $token['create_time'] = time();
            $token['end_time'] = $token['create_time'] + $token['expires_in'];

            //写入token表
            Db::name('token')->insert($token);

            $msg['msg'] = '登录成功';
            $msg['token'] = $token_str;
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '密码错误';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 前台登录
     * @description 前台登录
     * @author 李锐志
     * @url /login/login/login
     * @method *
     * @param name:phone require:1 default: desc:手机
     * @param name:password require:1 default: desc:密码
     */
    public function login()
    {
        $data = input();

        $validate = new Validate([
            'phone' => 'require',
            'password' => 'require',
        ]);

        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        $check = get_curl('user', 'user/info', ['phone' => $data['phone']]);

        //判断账号是否存在
        if (!$check['status']) {
            $msg['msg'] = '账号不存在';
            return $this->errorJson($msg);
        }

        $user = $check['data'];

        //账号存在,验证密码
        if ($user['password'] === md5($data['password'])) {

            //随机生成20位字符串
            $token_str = substr(str_shuffle('abcedfghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 20);

            //token表需要的字段
            $token['user_id'] = $user['id'];
            $token['token'] = $token_str;
            $token['expires_in'] = 3600;             //有效期60分钟
            $token['create_time'] = time();
            $token['end_time'] = $token['create_time'] + $token['expires_in'];

            //写入token表
            Db::name('token')->insert($token);

            $msg['msg'] = '登录成功';
            $msg['token'] = $token_str;
            $msg['user_info'] = get_curl('user', 'user/token_get_user_information', ['token' => $token_str])['data'];
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '密码错误';
            return $this->errorJson($msg);
        }
    }
}